package com.hytc.mymall.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hytc.mymall.R;

/**
 * Created by Administrator on 2016/12/1.
 */

public class ToastUtils {
    private static Toast imageTost;
    private static Toast longToast;
    private static Toast toast;

    public static Dialog createLoadingDialog(final Context context, final String text) {
        final View inflate = LayoutInflater.from(context).inflate(R.layout.loading_dialog, (ViewGroup)null);
        final LinearLayout linearLayout = (LinearLayout)inflate.findViewById(R.id.dialog_view);
        final ImageView imageView = (ImageView)inflate.findViewById(R.id.img);
        final TextView textView = (TextView)inflate.findViewById(R.id.tipTextView);
        imageView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.loading_animation));
        textView.setText((CharSequence)text);
        final Dialog dialog = new Dialog(context, R.style.loading_dialog);
        dialog.setCancelable(false);
        dialog.setContentView((View)linearLayout, (ViewGroup.LayoutParams)new LinearLayout.LayoutParams(-1, -1));
        return dialog;
    }

    public static void showToast(final Context context, final Object o) {
        String string;
        if (o == null) {
            string = "";
        }
        else {
            string = o.toString();
        }
        if (ToastUtils.toast == null) {
            (ToastUtils.toast = Toast.makeText(context, (CharSequence)string, Toast.LENGTH_LONG)).setText((CharSequence)string);
        }
        else {
            ToastUtils.toast.setText((CharSequence)string);
        }
        ToastUtils.toast.show();
    }

    public static void showToast(final Context context, final Object o, final boolean b) {
        String string;
        if (o == null) {
            string = "";
        }
        else {
            string = o.toString();
        }
        if (b) {
            if (ToastUtils.longToast == null) {
                ToastUtils.longToast = Toast.makeText(context, (CharSequence)string, Toast.LENGTH_LONG);
            }
            ToastUtils.longToast.setText((CharSequence)string);
            ToastUtils.longToast.show();
            return;
        }
        showToast(context, string);
    }

    public static void showToast(final Context context, final String s) {
        if (ToastUtils.toast == null) {
            (ToastUtils.toast = Toast.makeText(context, (CharSequence)s, Toast.LENGTH_SHORT)).setText((CharSequence)s);
        }
        else {
            ToastUtils.toast.setText((CharSequence)s);
        }
        ToastUtils.toast.show();
    }

    public static void showToast(final Context context, final String text, final boolean b) {
        if (b) {
            if (ToastUtils.longToast == null) {
                ToastUtils.longToast = Toast.makeText(context, (CharSequence)text, Toast.LENGTH_LONG);
            }
            ToastUtils.longToast.setText((CharSequence)text);
            ToastUtils.longToast.show();
            return;
        }
        showToast(context, text);
    }

    public static void showToastWithImage(Context context, final String text, final boolean b) {
        if (ToastUtils.imageTost == null) {
            (ToastUtils.imageTost = Toast.makeText(context, (CharSequence)text, Toast.LENGTH_LONG)).setGravity(17, 0, 0);
            final LinearLayout linearLayout = (LinearLayout)ToastUtils.imageTost.getView();
            final ImageView imageView = new ImageView(context);
            int imageResource;
            if (b) {
                //测试图
                imageResource = R.drawable.loading1;
            }
            else {
                imageResource = R.drawable.loading2;
            }
            imageView.setImageResource(imageResource);
            linearLayout.addView((View)imageView, 0);
        }
        else {
            ToastUtils.imageTost.setText((CharSequence)text);
            final LinearLayout linearLayout2 = (LinearLayout)ToastUtils.imageTost.getView();
            linearLayout2.removeViewAt(0);
            final ImageView imageView2 = new ImageView(context);
            int imageResource2;
            if (b) {
                //测试图
                imageResource2 = R.drawable.loading1;
            }
            else {
                imageResource2 = R.drawable.loading2;
            }
            imageView2.setImageResource(imageResource2);
            linearLayout2.addView((View)imageView2, 0);
        }
        ToastUtils.imageTost.show();
    }
}

